package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.CustomerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;


    @Override
    public Map<String, Object> getCustomerList(Integer page, Integer rows, String customerName) {
        Map<String, Object> map = new HashMap<>();

        Page<Customer> page1 = PageHelper.startPage(page, rows);
        List<Customer> goodsList = customerDao.selectCustomerList(customerName);

        map.put("total", page1.getTotal());
        map.put("rows", goodsList);
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Customer customer) {
        Integer customerId = customer.getCustomerId();
        //id不为空时 更新信息
        if (customerId != null) {
            customerDao.updateById(customer);
            return;
        } else {
            //id 为空 新增数据
            customerDao.saveCustomer(customer);
        }
    }

    @Override
    public void deleteByIds(String ids) {
        String[] idsArr = ids.split(",");

        customerDao.deleteByIds(idsArr);
    }
}
